package NO6_Tree;

/**
 * 2025-08-15
 * 力扣 - 101. 对称二叉树
 * <p>
 * 给你一个二叉树的根节点 root ， 检查它是否轴对称。
 */
public class IsSymmetricMain {
    public static void main(String[] args) {
        TreeNode root = new TreeNode(1, new TreeNode(2, new TreeNode(3), new TreeNode(4)), new TreeNode(2, new TreeNode(4), new TreeNode(3)));
        System.out.println(isSymmetric(root));
    }

    public static boolean isSymmetric(TreeNode root) {
        if (root == null) {
            return true;
        }
        return isMirror(root.left, root.right);
    }

    private static boolean isMirror(TreeNode left, TreeNode right) {
        // 两个都为空
        if (left == null && right == null) return true;
        // 只有一个为空
        if (left == null || right == null) return false;
        // 值不相等
        if (left.val != right.val) return false;
        // 递归比较：左子树的左 vs 右子树的右 && 左子树的右 vs 右子树的左
        return isMirror(left.left, right.right) && isMirror(left.right, right.left);
    }
}
